'''
Created on 01/04/2010

@author: miguel
'''
import pygame
import random
from PlayerPanel import PlayerPanel
from BugExit import BugExit
from Instruction import Instruction
from Bug import Bug
from GameBoard import GameBoard
from Box import Box
from Tree import Tree

class Player(object):
    (ALIVE, DEAD) = range(2)

    def __init__(self, screen, game, field):
        self.screen = screen
        self.game = game
        self.field = field

        '''create player panel'''
        self.player_panel = PlayerPanel(self.screen, self.game)
        '''create bug exit'''
        self.bug_exits = pygame.sprite.Group()
        self.bug_exits.add(BugExit(self.screen, self.game, self.field, BugExit.USER))
        '''initialize the user boxes group (empty)'''
        self.user_boxes = pygame.sprite.Group()
        '''initialize the user bugs group (empty)'''
        self.user_bugs = pygame.sprite.Group()
        '''number of bugs'''
        self.bugs_number = 0
        self.__init_user_boxes_user_trees()
        self.status = self.ALIVE
        self.supports_discovered = 0
        self.surprises_discovered = 0

    def support_discovered(self):
        self.supports_discovered = self.supports_discovered + 1
        self.player_panel.set_support_discovered()

    def surprise_discovered(self):
        self.surprises_discovered = self.surprises_discovered + 1
        self.player_panel.set_surprise_discovered()

    def update(self, time_passed):
        if self.supports_discovered == 3:
            self.status = self.DEAD

        for user_bug in self.user_bugs:
            user_bug.update(time_passed)

        self.player_panel.update()

    def create_user_bug(self, instruction_list):
        if self.bugs_number < 2:
            self.bugs_number = self.bugs_number + 1
            self.user_bugs.add(Bug(self.screen, self.game, self.field, (270, 510), instruction_list, GameBoard.USER_BUG))

    def get_status(self):
        return self.status

    def __init_user_boxes_user_trees(self):

        self.boxes_list = [Box.SUPPORT, Box.SUPPORT, Box.SUPPORT, Box.SURPRISE2, Box.SURPRISE1]
        self.box_estructure = [[(0, 510), (60, 570), (210, 540), (540, 570), (480, 540)],
                               [(0, 570), (90, 510), (390, 570), (540, 510), (480, 570)],
                               [(0, 510), (210, 510), (330, 510), (540, 510), (420, 570)],
                               [(30, 540), (180, 570), (210, 540), (360, 570), (540, 570)]]
        self.tree_estructure = [[(0, 480), (60, 540), (210, 510), (540, 540), (480, 510)],
                                [(0, 540), (90, 480), (390, 540), (540, 480), (480, 540)],
                                [(0, 480), (210, 480), (330, 480), (540, 480), (420, 540)],
                                [(30, 510), (180, 540), (210, 510), (360, 540), (540, 540)]]

        self.estructure_selected = random.randint(0, len(self.box_estructure) - 1)
        for object in self.boxes_list:
            rand = random.randint(0, len(self.box_estructure[self.estructure_selected]) - 1)
            pos = self.box_estructure[self.estructure_selected][rand]
            self.user_boxes.add(Box(self.screen, self.game, self.field, pos, object, GameBoard.USER_BOX))
            self.box_estructure[self.estructure_selected].pop(rand)

        for object in self.tree_estructure[self.estructure_selected]:
            self.game.trees.add(Tree(self.screen, self.game, self.field, object))


